package com.foyekoo.project001.model;

import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import com.foyekoo.project001.command.Command;
import com.foyekoo.project001.config.LocalClient;
import com.foyekoo.project001.config.SpUtil;

import java.text.DecimalFormat;

import javax.net.ssl.SSLPeerUnverifiedException;

/**
 * ${huneng} on 2018/8/1 16:52
 */

public class GameSetVM {

    /*
      *多少秒一局
     */
    public ObservableField<String> gameTimeByOne = new ObservableField<>();
    public ObservableInt gameTimeByOneProgress = new ObservableInt(1);

    /**
     * 多少币一局
     */
    public ObservableField<String> gameCoinByOne = new ObservableField<>();
    public ObservableInt gameCoinByOneProgress = new ObservableInt(1);

    public ObservableField<String> zon1Powder = new ObservableField<>();
    public ObservableInt zon1PowerProgress = new ObservableInt(1);

    public ObservableField<String> zon2Powder = new ObservableField<>();
    public ObservableInt zon2PowerProgress = new ObservableInt(1);

    public ObservableField<String> zon1Waiting = new ObservableField<>();
    public ObservableInt zon1WaitProgress = new ObservableInt(1);

    public ObservableField<String> zon2Waiting = new ObservableField<>();
    public ObservableInt zon2WaitProgress = new ObservableInt(1);

    public ObservableField<String> zSpeed = new ObservableField<>();
    public ObservableInt zSpeedProgress = new ObservableInt(1);

    public ObservableField<String> ySpeed = new ObservableField<>();
    public ObservableInt ySpeedProgress = new ObservableInt(1);

    public ObservableField<String> zon1Push = new ObservableField<>();
    public ObservableInt zon1PushProgress = new ObservableInt(1);

    public ObservableField<String> zon2Push = new ObservableField<>();
    public ObservableInt zon2PushProgress = new ObservableInt(1);

    public void powerSet(){
        int z1 = Integer.parseInt(zon1Powder.get());
        int z2 = Integer.parseInt(zon2Powder.get());
        Command.machineryPower(z1,z2);
    }

    public void waitingSet(){
        double zon1 = Double.parseDouble(zon1Waiting.get());
        double zon2 = Double.parseDouble(zon2Waiting.get());

        int z1 = (int) (zon1 * 10);
        int z2 = (int) (zon2 * 10);

        Command.machineryWaiting(z1,z2);
    }

    public GameSetVM(){
        zSpeed.set(SpUtil.getInstance().getLocalClient().left_Right_Speed + "");
        zSpeedProgress.set(SpUtil.getInstance().getLocalClient().left_Right_Speed );
        ySpeed.set(SpUtil.getInstance().getLocalClient().up_Down_Speed +"");
        zSpeedProgress.set(SpUtil.getInstance().getLocalClient().up_Down_Speed);
        zon1Powder.set(SpUtil.getInstance().getLocalClient().power_Z1 + "");
        zon1PowerProgress.set(SpUtil.getInstance().getLocalClient().power_Z1 );
        zon2Powder.set(SpUtil.getInstance().getLocalClient().power_Z2 + "");
        zon2PowerProgress.set(SpUtil.getInstance().getLocalClient().power_Z2);
        zon1Waiting.set((SpUtil.getInstance().getLocalClient().waitingTime_Z1  / 10) + "");
        zon2Waiting.set((SpUtil.getInstance().getLocalClient().waitingTime_Z2  / 10) + "");
        zon1Push.set(SpUtil.getInstance().getLocalClient().strongPush_Z1 + "");
        zon2Push.set(SpUtil.getInstance().getLocalClient().strongPush_Z2 + "");
        gameCoinByOne.set(SpUtil.getInstance().getLocalClient().coinByOneGame + "");
        gameTimeByOne.set(SpUtil.getInstance().getLocalClient().gameTime + "");

    }

    /**
     * 时间设置
     */
    public void gameTimeSet(){

        Command.gameTimeSet(Integer.parseInt(gameTimeByOne.get()));
    }

    /**
     * 硬币设置
     */
    public void coinSet(){
        Command.gameCoinSet(Integer.parseInt(gameCoinByOne.get()));
    }

    /**
     * 推送设置
     */
    public void pushSet(){
        int z1= Integer.parseInt(zon1Push.get());
        int z2 = Integer.parseInt(zon2Push.get());
        Command.machineryPush(z1,z2);
    }

    /**
     * z
     */
    public void speedSet(){
        int z = Integer.parseInt(zSpeed.get());
        int y = Integer.parseInt(ySpeed.get());
        Command.machinerySpeed(z,y);
    }



    public void timeByOneChange(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress <= 0) return;
        gameTimeByOne.set(progress + "");
        gameTimeSet();
    }

    public void coinByOneChange(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress <= 0) return;
        gameCoinByOne.set(progress + "");
        coinSet();
    }

    public void ySpeedChange(SeekBar seekBar, int progress, boolean fromUser) {
        ySpeed.set(progress + "");

        speedSet();
    }

    public void zSpeedChange(SeekBar seekBar, int progress, boolean fromUser) {
        zSpeed.set((progress)+ "");

        speedSet();
    }

    public void z1waitingChange(SeekBar seekBar, int progress, boolean fromUser) {


        String result = "";
        float num =(float)progress/10;

        DecimalFormat df = new DecimalFormat("0.0");

        result = df.format(num);
        zon1Waiting.set( result);
        waitingSet();
    }

    public void z2waitingChange(SeekBar seekBar, int progress, boolean fromUser) {
        String result = "";
        float num =(float)progress/10;

        DecimalFormat df = new DecimalFormat("0.0");

        result = df.format(num);
        zon2Waiting.set( result);
        waitingSet();
    }

    public void z1powerChange(SeekBar seekBar, int progress, boolean fromUser) {
        zon1Powder.set( progress+ "");
        powerSet();
    }


    public void z2powerChange(SeekBar seekBar, int progress, boolean fromUser) {
        zon2Powder.set( progress + "");
        powerSet();
    }

    public void z1pushChange(SeekBar seekBar, int progress, boolean fromUser) {
        zon1Push.set(progress + "");
        pushSet();
    }

    public void z2pushChange(SeekBar seekBar, int progress, boolean fromUser) {
        zon2Push.set(progress + "");
        pushSet();
    }


    public void freeMole(CompoundButton compoundButton , boolean isCheck){
        Command.freeMode(isCheck);//免费模式
        LocalClient client = SpUtil.getInstance().getLocalClient();
        client.freeMode = isCheck ? 1 : 0;
        SpUtil.getInstance().saveCurrentClient(client);
    }

}
